<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>yukari world</title>
<#include "/lib/base_source.ftl">
<style type="text/css">
</style>
</head>
<body>
<div class='whole-container'>
<#include "/lib/header.ftl">
    <div>
        <ul class="breadcrumb">
        	<li>贡品管理</li><span class="divider">/</span>
            <li>修改任务</li>
        </ul>
    </div>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="container span12">
            <#include "/lib/alert.ftl">

                <div class='main-content'>
                    <div class="well form-horizontal">
                        <div class='main-action'>
                        	<form id="addForm" action="" method="post">

                        		<div class="control-group">
	                                <label class="control-label">品牌</label>
	                                <div class="controls">
										<input id="brand" type="text" name="brand"/>
	                                </div>
	                            </div>
                        		<div class="control-group">
	                                <label class="control-label">名字</label>
	                                <div class="controls">
										<input id="name" type="text" name="name" />
	                                </div>
	                            </div>
                        		<div class="control-group">
	                                <label class="control-label">价格</label>
	                                <div class="controls">
										<input id="price" type="text" name="price" />
	                                </div>
	                            </div>
                        		<div class="control-group">
	                                <label class="control-label">图片</label>
	                                <div class="controls" id="imageDiv">
                                        <input id="imageFile" type="file" onchange="uploadCosmeticsImage()"/>
										<img id="imageSrc" src="" style="display: block" />
                                        <input id="image" type="hidden" name="image" value=""/>
                                        <span id="uploadStatus" style="color:red;"></span>
	                                </div>
	                            </div>
                        		<div class="control-group">
	                                <label class="control-label">购买地址</label>
	                                <div class="controls">
										<input id="address" type="text" name="address"/>
	                                </div>
	                            </div>
                        		<div class="control-group">
	                                <label class="control-label">备注</label>
	                                <div class="controls">
										<textarea name="note" rows="10" ></textarea>
	                                </div>
	                            </div>
                                <div class="control-group">
                                    <label class="control-label"></label>
                                    <div class="controls">
                                        <a href="javascript:" onclick="addCosmetics()" class="btn btn-sm btn-info">添加</a>
                                    </div>
                                </div>
                			</form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
	var uploadFlag = true;
	function uploadCosmeticsImage(){
        var imageFile = $("#imageFile")[0].files[0]
		if (imageFile) {
			if (uploadFlag){
				uploadFlag = false;
				var formData = new FormData();
				formData.append("image",imageFile);
				formData.append("type","cosmetics");
				$.ajax({
					url : "${rc.contextPath}/admin/upload/image",
					type : 'POST',
					data : formData,
					processData : false,
					contentType : false,
					beforeSend:function(){
						$("#uploadStatus").html("正在上传，请稍后...")
					},
					success : function(result) {
						if (result.code == 0){
                            $("#imageSrc").attr("src","http://image.loveyukari.com/"+result.data);
                            $("#imageSrc").show();
                            $("#image").val(result.data);
                            $("#uploadStatus").html("");
                            $("#imageFile").hide();
						}
						uploadFlag = true;
					},
					error : function(responseStr) {
						$("#uploadStatus").html("上传失败")
						uploadFlag = true;
					}
				});
			}
        }
		
	}

	var addFlag = true;
	function addCosmetics(){
	    if (addFlag){
	        addFlag = false;
            $.ajax({
                url : "${rc.contextPath}/admin/cosmetics/add",
                dataType : "json",
                data :  $("#addForm").serialize(),
                type:'post',
                success:function(json){
                    if (json.code == 0){
                        location.href = "${rc.contextPath}/admin/cosmetics/list";
                    } else {
                        addFlag = true;
                        alert("添加失败: "+json.message);
                    }
				}
            });
		}
	}

</script>
</body>
</html>